<!-- 备件管理-备件基础数据页面-类型 -->
<template>
  <div>
    <px-form class="custom-form-ui equip-form" :model="queryParams" label-position="right" style="padding-top: 14px" label-width="120px">
      <px-row :gutter="16">
        <px-col :span="6"
          ><px-form-item label="类型名称">
            <px-input
              v-model="queryParams.typeName"
              placeholder="请输入类型名称"
              maxlength="50"
              clearable
              @blur="queryParams.typeName = $event.target.value.trim()"
              @keyup.enter="queryTable"
            >
              <template #suffix>
                <px-icon style="cursor: pointer" @click="queryTable">
                  <search />
                </px-icon>
              </template>
            </px-input>
          </px-form-item>
        </px-col>
        <px-col :span="6">
          <EquipClassSelect :className="queryParams.className" @changeClassName="changeClassName" />
        </px-col>
        <px-col :span="6">
          <px-button type="primary" @click="queryTable">查询</px-button>
          <px-button @click="resetTable">重置</px-button>
        </px-col>
      </px-row>
    </px-form>
    <div class="equip-table-btn">
      <px-button v-has="'ALL_EQUIP_SPAREPARTS_BASEDATA_ADD'" style="margin-right: 8px" type="primary" @click="addSpareParts">新增类型</px-button>
    </div>
    <px-table row-key="id" :data="tableDataFiltered" class="equip-table" size="small" height="calc(100vh - 297px)" border>
      <px-table-column
        v-for="(item, index) in tableColumns"
        :key="index"
        :label="item.label"
        :min-width="item.minWidth"
        :align="item.align"
        :show-overflow-tooltip="item.showOverflowTooltip"
        :fixed="item.fixed"
      >
        <template #default="scope">{{
          scope.row[item.prop] === 0 ? scope.row[item.prop] : scope.row[item.prop] ? scope.row[item.prop] : "-"
        }}</template>
      </px-table-column>
      <px-table-column fixed="right" label="操作" width="150" align="center">
        <template #default="scope">
          <px-tooltip content="编辑" placement="top">
            <px-button v-has="'ALL_EQUIP_SPAREPARTS_BASEDATA_EDIT'" link type="primary" icon="EditPen" @click="handleEdit(scope.row)" />
          </px-tooltip>
          <px-tooltip content="添加子类型" placement="top">
            <px-button v-has="'ALL_EQUIP_SPAREPARTS_BASEDATA_ADD_CHILDREN'" link type="primary" icon="Add" @click="addSparePartsChild(scope.row)" />
          </px-tooltip>
          <px-tooltip content="删除" placement="top">
            <px-button v-has="'ALL_EQUIP_SPAREPARTS_BASEDATA_DELETE'" link type="danger" icon="Delete" @click="handleDelete(scope.row)" />
          </px-tooltip>
        </template>
      </px-table-column>
    </px-table>
    <!-- 新增/修改抽屉 -->
    <AddSparePartsDrawer
      :visible="sparePartsVisible"
      :drawerType="drawerType"
      :typeTitle="typeTitle"
      :sparePartsRow="sparePartsRow"
      :treeData="tableData"
      @queryTable="queryTable"
      @closeDrawer="closeDrawer"
    />
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "ALL_EQUIP_SPAREPARTS_BASEDATA"
});
import { queryListTree_1, delSparePartType } from "@/api/equip/spareParts";
import { PxMessage, PxMessageBox } from "px-ui";
import { onMounted, reactive, toRefs, computed } from "vue";
import EquipClassSelect from "@/views/all/equip/components/EquipClassSelect.vue";
import AddSparePartsDrawer from "./AddSparePartsDrawer.vue";
// import ViewSparePartsDialog from "./ViewSparePartsDialog.vue";

// const { proxy }: any = getCurrentInstance();
const state = reactive<any>({
  queryParams: {
    typeOrCode: "",
    pageNum: 1,
    pageSize: 20
  },
  tableColumns: [
    { prop: "name", label: "类型名称", minWidth: "240", showOverflowTooltip: true },
    { prop: "categoryDesc", label: "种类", minWidth: "120", showOverflowTooltip: true },
    { prop: "classNameDesc", label: "科室", minWidth: "120", showOverflowTooltip: true },
    { prop: "createName", label: "创建人", minWidth: "120", showOverflowTooltip: true },
    { prop: "createTime", label: "创建时间", minWidth: "120", showOverflowTooltip: true }
  ],
  tableData: [],
  total: 0,
  sparePartsVisible: false,
  drawerType: "add",
  typeTitle: "",
  sparePartsRow: {}
});
const { queryParams, tableColumns, tableData, sparePartsVisible, drawerType, typeTitle, sparePartsRow } = toRefs(state);
// 根据类型名称筛选表格
const filterTableByTypeName = (arr, name) => {
  const newArr = arr.filter(item => {
    const flag = item.name.includes(name);
    if (flag) {
      return true;
    } else if (item.children) {
      item.children = filterTableByTypeName(item.children, name);
      return item.children.length > 0;
    }
  });
  return newArr;
};
// 进入页面时触发
onMounted(() => queryTable());
const tableDataFiltered = computed(() => {
  const name = queryParams.value.typeName;
  if (name) {
    const arr = [...tableData.value];
    const newArr = filterTableByTypeName(arr, name);
    return newArr;
  } else {
    return tableData.value;
  }
});
// 科室组件调用父组件
const changeClassName = val => {
  queryParams.value.className = val;
};
const props = defineProps(["activeName"]);
// 查询备件列表
const queryTable = async () => {
  const params = { ...queryParams.value, type: props.activeName };
  const data = await queryListTree_1(params);
  tableData.value = data || [];
};
// 重置筛选条件和备件列表
const resetTable = async () => {
  queryParams.value = {
    pageNum: queryParams.value.pageNum,
    pageSize: queryParams.value.pageSize
  };
  queryTable();
};
// 添加备件类型-显示抽屉
const addSpareParts = () => {
  drawerType.value = "add";
  typeTitle.value = "新增类型";
  sparePartsVisible.value = true;
  const sort = tableData.value.length ? tableData.value.length : 0;
  sparePartsRow.value = { sort };
};
// 添加备件子类型-显示抽屉
const addSparePartsChild = row => {
  drawerType.value = "addChild";
  typeTitle.value = "新增子类型";
  sparePartsVisible.value = true;
  const sort = row?.children?.length ? row?.children?.length : 0;
  sparePartsRow.value = { parentId: row.id, category: row.category, className: row.className, sort };
};
// 编辑备件-显示抽屉
const handleEdit = (row: any) => {
  drawerType.value = "edit";
  typeTitle.value = "编辑类型";
  sparePartsRow.value = { ...row };
  sparePartsVisible.value = true;
};
// 添加/编辑备件-关闭抽屉
const closeDrawer = () => {
  sparePartsVisible.value = false;
};
// 删除备件
const handleDelete = async (row: any) => {
  // 1、不存在未解除的关联数据
  // if (!row?.children?.length) {
  PxMessageBox.confirm(`是否删除该类型“${row.name}”`, "删除提示", {
    confirmButtonText: "确定删除",
    confirmButtonClass: "px-button--danger",
    cancelButtonText: "取消",
    type: "warning",
    draggable: true
  })
    .then(async () => {
      await delSparePartType(row.id);
      PxMessage.success("删除成功");
      queryTable();
    })
    .catch(() => {});
};
</script>

<style lang="scss" scoped>
@import url("@/views/all/equip/scss/table-list.scss");
</style>
